//登录界面脚本代码
var Login = function() {
	return {
		init : function() {
			//注册回车事件
			$('body').keypress(function(e) {
				if (e.which == 13) {
					$("#login_ok").trigger("click");
				}
			});
			//验证码绑定点击事件
			$("#captcha_img").click(function(){
				var rd = Math.random() * 9 + 1;
				$("#captcha_img").attr("src","getValidateCode?randomKey="+rd);
				$("#j_captcha").val("");
				$(".validateCodeVI").removeClass();
			});
			//绑定验证码验证
			$("#j_captcha").on("input",function(e){
				var val = $(this).val();
				if(val.length==4){
					validateCode(val,true);
				}else{
					$(".validateCodeVI").removeClass("right");
					$(".validateCodeVI").addClass("error");
				}
			});
			//绑定登录事件
			$("#login_ok").click(function(){
				var issubmit = true;
				var i_index  = -1;
				var form = "#login_form";
				$(form).find('.in').each(function(i){
					if ($.trim($(this).val()).length == 0) {
						$(this).css('border', '1px #ff0000 solid');
						issubmit = false;
						if (i_index == -1)
							i_index  = i;
					}
				});
				if (!issubmit) {
					$(form).find('.in').eq(i_index).focus();
					return;
				}
				var $remember = $("#j_remember");
				if ($remember.attr('checked')) {
					$.cookie(COOKIE_NAME, $("#j_userCode").val(), { path: '/', expires: 15 });
				} else {
					$.cookie(COOKIE_NAME, null, { path: '/' });  //删除cookie
				}
				var code = $("#j_captcha").val();
				$("#message").text("");
				//验证码验证
				/**
				if(!validateCode(code,false)){
					$("#j_captcha").css('border', '1px #ff0000 solid').focus();;
					return;
				}**/
				//$("#login_ok").attr("disabled", true).val('登陆中..');
				var userCode = $("#j_userCode").val()
				var password = $.base64.encode(userCode+$("#j_password").val())
				//var password = HMAC_SHA256_MAC(userCode, $("#j_password").val());
				//$("#j_password").val(HMAC_SHA256_MAC($("#j_randomKey").val(), password));
				//$("#j_password").val(password);
				//$("#login_form").submit();
				//进行同步后台验证
		    	$.ajax({
		    		type:"post",
		    		url:"login",
		    		data:{userCode:userCode,password:password,validateCode:code,
		    			jfinal_token:$("#jfinal_token").val()},
		    		dataType:"json",
		    		success:function(data){
		    			//alert(JSON.stringify(data));
		    			if(data.result==true){
		    				/**
		    				$.gritter.add({
		    					title:	'登录失败提示！',
		    					text:	data.msg,
		    					fade_out_speed:500,
		    					time:2000,
		    					sticky: false
		    				});
		    				**/
		    				//跳转到主页
		    				window.location.href="index";
		    			}else{
		    				showMsg(data.message);
		    			}
		    		},
		    		async:false
		    	});
			});
			
		}
	};
}();
//进行验证码验证
function validateCode(code,async){
	var r = false;
	$.ajax({
		type:"post",
		url:"validateCode",
		data:{validateCode:code},
		dataType:"json",
		success:function(data){
			r = data;
			if(data){
				$(".validateCodeVI").removeClass("error");
				$(".validateCodeVI").addClass("right");
			}else{
				$(".validateCodeVI").removeClass("right");
				$(".validateCodeVI").addClass("error");
			}
			
		},
		async:async
	});
	return r;
}
/**
 * 显示消息
 * @param msg
 */
var $messageBox = $("div.messageBox");
$messageBox.hide();
var timer;
function showMsg(msg){
	//将上次未消失的消息框
	$messageBox.hide();
	//取消定时器
	window.clearInterval(timer);
	//设置消失
	$messageBox.text(msg);
	//淡入到界面
	$messageBox.fadeIn("normal",function(){
		//2秒后自动淡出界面
		timer = window.setInterval(function(){
			$messageBox.fadeOut("normal");
		},2000);
	});
}